package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.Role;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.role.*;
import com.filldream.fastboot.base.request.pc.roleMenu.SetRoleMenuSTO;
import com.filldream.fastboot.base.response.pc.MenusDTO;
import com.filldream.fastboot.base.response.pc.RoleDetailDTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.exception.GlobalException;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.manager.RoleMenuManager;
import com.filldream.fastboot.core.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "PC-角色管理")
@RestController
@RequestMapping("/pc/role")
public class PcRoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private RoleMenuManager roleMenuManager;

    @Log(value = "查询角色列表")
    @ApiOperation(value = "角色列表",notes = "可用于下拉框选择,用于下拉时请将pageSize设置的大一点")
    @GetMapping("/list")
    @RolePermissions("role:list")
    public P<Role> list(@Valid RoleListSTO roleListSTO) throws GlobalException {
        return roleService.queryPageList( roleListSTO );
    }

    @Log(value = "不带权添加角色")
    @ApiOperation(value = "不带权添加角色",notes = "不带权限添加角色")
    @PostMapping("/add")
    @RolePermissions("role:add")
    public R<Long> add(@RequestBody @Valid AddRoleSTO addRoleSTO) throws GlobalException {
        Long pcRoleId = LoginHolder.getPcRoleId();
        Role role = TransferUtil.changeToObj(addRoleSTO, Role.class);
        role.setId(null).setPId(pcRoleId);
        return R.autoSuccess(roleService.save(role) ? role.getId() : null);
    }

    @Log(value = "带权添加角色")
    @ApiOperation(value = "带权添加角色",notes = "带权限添加角色")
    @PostMapping("/complexAdd")
    public B complexAdd(@RequestBody @Valid ComplexAddRoleSTO complexAddRoleSTO) throws GlobalException {
        complexAddRoleSTO.setId(null);
        complexAddRoleSTO.check();
        AddRoleSTO addRoleSTO = TransferUtil.changeToObj(complexAddRoleSTO, AddRoleSTO.class);
        R<Long> addResult = add(addRoleSTO);
        Assert.isOk( addResult, ErrorCode.BASE_ERR.reMsg("添加角色失败"));
        SetRoleMenuSTO setRoleMenuSTO = new SetRoleMenuSTO();
        setRoleMenuSTO.setRoleId( addResult.getData() );
        setRoleMenuSTO.setRoleMenuList( complexAddRoleSTO.getRoleMenuList() );
        return setRoleMenu(setRoleMenuSTO);
    }

    @Log(value = "带权修改角色")
    @ApiOperation(value = "带权修改角色",notes = "带权限修改角色")
    @PutMapping("/complexAdd")
    @RolePermissions(value = "role:complexAdd")
    public B complexAdd2(@RequestBody @Valid ComplexAddRoleSTO complexAddRoleSTO) throws GlobalException {

        ModifyRoleSTO role = new ModifyRoleSTO();
        role.setRoleName( complexAddRoleSTO.getRoleName() );
        role.setMemo( complexAddRoleSTO.getMemo() );
        role.setId( complexAddRoleSTO.getId() );
        role.setDataScope(complexAddRoleSTO.getDataScope());
        modify(role);

        //修改权限
        SetRoleMenuSTO roleMenuSTO = new SetRoleMenuSTO();
        roleMenuSTO.setRoleId(complexAddRoleSTO.getId());
        roleMenuSTO.setRoleMenuList( complexAddRoleSTO.getRoleMenuList() );
        setRoleMenu(roleMenuSTO);
        return setRoleMenu(roleMenuSTO);
    }

    @Log(value = "修改角色")
    @ApiOperation(value = "修改角色",notes = "修改角色")
    @PostMapping("/modify")
    @RolePermissions("role:modify")
    public B modify(@RequestBody @Valid ModifyRoleSTO modifyRoleSTO) throws GlobalException {
        Role role = new Role();
        BeanUtils.copyProperties(modifyRoleSTO,role);
        return roleService.modify(role);
    }

    @Log(value = "删除角色")
    @ApiOperation(value = "删除角色",notes = "删除角色,传入角色ID")
    @PostMapping("/del")
    @RolePermissions("role:del")
    public B del(@RequestBody  IdSTO idSTO)  {
        return roleService.delRole(idSTO.getId());
    }

    @Log(value = "设置权限")
    @ApiOperation(value = "设置权限",notes = "逗号隔开(所有已选的子级菜单ID)")
    @PostMapping("/setRoleMenu")
    @RolePermissions("role:setRoleMenu")
    public B setRoleMenu(@RequestBody SetRoleMenuSTO setRoleMenuSTO) throws GlobalException {
        return roleService.setRoleMenu(setRoleMenuSTO);
    }

    @Log(value = "角色已有菜单")
    @ApiOperation(value = "角色已有菜单")
    @PostMapping("/getRoleSelectedMenu")
    @RolePermissions("role:getRoleSelectedMenu")
    public R<MenusDTO> getRoleSelectedMenu(@RequestBody @Valid RoleIdSTO roleIdSTO){
        return R.autoSuccess(roleMenuManager.getRoleSelectedMenu(roleIdSTO.getRoleId()));
    }

    @RolePermissions
    @ApiOperation(value = "当前登陆人所有权限")
    @GetMapping("/routes")
    public R<MenusDTO> routes(){
        return R.autoSuccess(roleMenuManager.getRoleSelectedMenu(LoginHolder.getPcRoleId()));
    }

    @Log(value = "角色可选菜单")
    @ApiOperation(value = "角色可选菜单")
    @PostMapping("/getRoleOptionalMenu")
    @RolePermissions(value = "role:getRoleOptionalMenu")
    public R<MenusDTO> getRoleOptionalMenu(){
        return R.autoSuccess(roleMenuManager.getRoleOptionalMenu(true,LoginHolder.getPcRoleId()));
    }

    @Log(value = "角色详情")
    @ApiOperation(value = "角色详情")
    @PostMapping("/detail")
    @RolePermissions(value = "role:detail")
    public R<RoleDetailDTO> detail(@RequestBody @Valid RoleIdSTO roleIdSTO){
        Role role = roleService.getById(roleIdSTO.getRoleId());
        RoleDetailDTO result = TransferUtil.changeToObj(role, RoleDetailDTO.class);
        result.setMenus(roleMenuManager.getRoleOptionalMenu(role.getId().equals(LoginHolder.getPcRoleId()) && !role.getId().equals(1L),roleIdSTO.getRoleId()));
        return R.autoSuccess(result);
    }

}
